/*
 * @Autor: zengy
 * @Date: 2021-03-31 16:08:58
 * @LastEditors: tlp
 * @LastEditTime: 2021-08-30 14:23:29
 * @Description:
 * @FilePath: \3DViewer\src\store\index.js
 */

/**
 import { useStore } from 'vuex'
 const store = useStore()
 // 在 computed 函数中访问一个 state
      count: computed(() => store.state.count),

// 在 computed 函数中访问一个 getter
double: computed(() => store.getters.double)

// 访问一个 mutation
increment: () => store.commit('increment'),

// 访问一个 action
asyncIncrement: () => store.dispatch('asyncIncrement')
*/
import { createStore } from 'vuex';
import bus from '@p/bus';
import axios from '@p/axios';
import api from '@/api/interface';

export default createStore({
  state() {
    return {
      wsEventBus: new bus(), // 全局ws的eventBus
      WPFEventBus: new bus(), //全局WPFEventBus
      ws: null, // 全局websocket自定义实例
      px: 0, // html字体大小
      token: 'xxxtoken', // 登录token
      tokenType: 'bearer', // 登录token_type
      stationId: 81, // 当前车站ID
      stationName: '陈头岗', //当前车站名称
      lineId: 18, //当前线路id
      systemList: [], // 专业列表
      lineStationList: [], // 线路车站数据
      alarmLevelList: [], // 线路车站数据
      broadcastType: 0, // 广播类型-PA | PIDS
      broadcastSelectId: [],
      toilet: [
        { type: 1, name: '男厕所', iconname: 'iconnancesuo', typename: 'man' },
        { type: 2, name: '女厕所', iconname: 'iconnvcesuo', typename: 'woman' },
        { type: 3, name: '母婴厕所', iconname: 'iconicon-test', typename: 'mbr' },
        { type: 4, name: '无障碍卫生间情况', iconname: 'iconcanjiren', typename: 'drm' }
      ] //智能卫生间类型
    };
  },
  mutations: {
    /**
     * @description: html基础px
     * @param {Number} px 像素
     * @return {*}
     */
    setPX(state, px) {
      state.px = px;
    },
    /**
     * @description: 保存wpf传递的基础数据
     * @param {String} token 登录token
     * @param {String} tokenType 登录tokenType
     * @param {Number} stationId 当前车站id
     * @param {String} stationName 当前车站名称
     * @param {Number} lineId 当前线路id
     * @return {*}
     */
    setBaseData(state, { token, tokenType, stationId, stationName, lineId }) {
      state.token = token;
      state.tokenType = tokenType;
      state.stationId = stationId;
      state.stationName = stationName;
      state.lineId = lineId;
    },
    /**
     * @description: 保存全局ws实例至store
     * @param {Object} ws
     * @return {*}
     */
    setWebSocket(state, ws) {
      state.ws = ws;
    },
    /**
     * @description: 保存当前线路Id及车站Id
     * @param {Number} stationId
     * @param {Array<Number>} lineId
     * @return {*}
     */
    setStationLine(state, { stationId, lineId }) {
      state.stationId = stationId;
      state.lineId = lineId;
    },
    /**
     * @description: 保存当前线路Id及车站Id
     * @param {Number} stationId
     * @param {Array<Number>} lineId
     * @return {*}
     */
    setSystemList(state, value) {
      state.systemList = value;
    },
    setLineStationList(state, data) {
      state.lineStationList = Array.isArray(data) ? data : [];
      // if (state.lineStationList.length > 0) {
      //   if (state.lineStationList.some(({ lineId }) => lineId === state.lineId)) return;
      //   state.lineId = state.lineStationList[0].lineId;
      // } else state.lineId = 18;
    },
    /**
     * @description: 保存告警级别
     * @param {Object} alarmLevelList
     * @return {*}
     */
    setAlarmLevelList(state, value) {
      state.alarmLevelList = value;
    },
    /**
     * @description:  广播类型- 0:PA | 1:PIDS
     * @param {Number} broadcastType
     * @return {*}
     */
    setBroadcastType(state, value) {
      state.broadcastType = value;
    },
    /**
     * @description:  广播选择
     * @param {Number}
     * @return {*}
     */
    setbroadcastSelectId(state, value) {
      state.broadcastSelectId = value;
    }
  },
  actions: {
    //专业数据
    setSystemList({ commit }) {
      axios.get(api.device.list, {}).then((data) => {
        commit('setSystemList', data); // 保存token至store
      });
    },
    //线路数据
    setLineStationList({ commit }) {
      axios.get(api.basic.lineStationList).then((data) => {
        commit('setLineStationList', data); // 保存token至store
      });
    },
    //线路数据
    setAlarmLevelList({ commit }) {
      axios.get(api.alarm.levelList).then((data) => {
        commit('setAlarmLevelList', data); // 保存token至store
      });
    }
  },
  getters: {
    lineStationListMap(state) {
      const map = new Map();
      state.lineStationList.forEach(({ lineId, stations }) => map.set(lineId, stations));
      return map;
    },
    stationList(state, getters) {
      const res = getters.lineStationListMap.get(state.lineId);
      return Array.isArray(res) ? res : [];
    }
  }
});
